import { JSEncrypt } from 'jsencrypt'

let publicKey = `-----BEGIN PUBLIC KEY-----
IKcleeAfYqjXhO/mrP6sA/nupiq7YH4MFBiwrAjpfkPXTxlbXVedHDO418AOAvjU
-----END PUBLIC KEY-----`

const encrypt = (data) => {
    var encryptor = new JSEncrypt()
    encryptor.setPublicKey(publicKey)

    if (typeof data == 'object') {
        data = JSON.stringify(data)
    }
    console.log(JSON.stringify(data))
    return encryptor.encrypt(data)
}

const decrypt = (encrypted) => {
    var decrypt = new JSEncrypt()
    var priKey = `-----BEGIN PRIVATE KEY-----
    8Rifx970adUMyzZskF/YLtMWOQ==
    -----END PRIVATE KEY-----`
    decrypt.setPrivateKey(priKey)
    var uncrypted = decrypt.decrypt(encrypted)
    console.log(uncrypted)
    try {
        const parsedResponse = JSON.parse(uncrypted);
        return parsedResponse;
    } catch (error) {
        return uncrypted;
    }
}

export {
    encrypt,
    decrypt,
}


